package logic

import (
	"context"
	"doctor/doctorrpc/doctorrpc"
	"doctor/doctorrpc/internal/svc"
	"doctor/models/mysql"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type DoctorAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDoctorAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoctorAddLogic {
	return &DoctorAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DoctorAddLogic) DoctorAdd(in *doctorrpc.DoctorAddRequest) (*doctorrpc.DoctorAddResponse, error) {
	// todo: add your logic here and delete this line

	var doctor mysql.Doctor

	mysql.DB.Debug().Where("name = ?", in.Name).Find(&doctor)

	if doctor.ID != 0 {
		return nil, fmt.Errorf("医生已存在,添加失败")
	}

	doctors := mysql.Doctor{
		Name:        in.Name,
		HeadImage:   in.HeadImage,
		Address:     in.Address,
		DoctorName:  in.DoctorName,
		RoomAddress: in.RoomAddress,
		Count:       in.Count,
	}

	err := mysql.DB.Debug().Create(&doctors).Error
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &doctorrpc.DoctorAddResponse{Id: int64(doctors.ID)}, nil
}
